<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery-1.12.4.min.js"></script>
    <!-- 引入模板引擎js文件 -->
    <script src="./libs/template-web.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <form class="form-inline">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <!-- <option value="">所有分类</option>
                            <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option> -->
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </form>
                </div>
                <div class="col-xs-6">
                    <a href="article_release.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th>标题</th>
                        <th>作者</th>
                        <th>分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr> -->
                </tbody>
            </table>
            <div class="row text-center">
              <ul id="pagination-demo" class="pagination-sm"></ul>
              <div class="box">亲，没有数据了哦！！！</div>
            </div>
        </div>
    </div>
</body>
<!-- 分页插件的js文件 -->
<script src="./libs/jquery.twbsPagination.js"></script>
<script src="./libs/http.js"></script>
<!-- 列表模板引擎 -->
<script type="text/html" id="option">
<option value="">所有分类</option>
{{each data}}
<option value = "{{$value.id}}">{{$value.name}}</option>
{{/each}}
</script>


<!-- 文章列表信息显示 -->
<script type="text/html"  id="artList">
{{each data.data}}
<tr>
    <td>{{$value.title}}</td>
    <td>{{$value.author}}</td>
    <td>{{$value.category}}</td>
    <td class="text-center">{{$value.date}}</td>
    <td class="text-center">{{$value.state}}</td>
    <td class="text-center">
        <a href="article_edit.html?id={{$value.id}}" class="btn btn-default btn-xs">编辑</a>
        <a href="javascript:void(0);" data-id='{{$value.id}}'  class="btn btn-danger btn-xs delete">删除</a>
    </td>
</tr>
{{/each}}
</script>

<script>
 $(function(){
//  下拉列表信息显示
$.get({
    url :  BigNew.category_list,
    success : function(res){
        if(res.code === 200){
            let html = template('option',res);
            $('#selCategory').html(html);
        }
    }
})

// 文章列表信息显示
let mypage = 1;
let perpage = 10;

// 将详情列表数据渲染封装
function getData(mypage,callback){
    $.get({
    url : BigNew.article_query,
    data : {
        type : $('#selCategory').val() ,
        state : $('#selStatus').val(),
        page : mypage,
        perpage : perpage ,
    },
    success : function(res){
       if(res.code === 200){
           let htmlSrc = template('artList',res);
           $('tbody').html(htmlSrc);

           if(callback != null && res.data.data.length != 0){
              callback(res);
              $('#pagination-demo').show();
              $('.box').hide();
           }else if(res.data.data.length == 0 && res.data.totalPage != 0 && res.data.totalPage == mypage - 1){
               mypage -= 1;
               $('#pagination-demo').twbsPagination('changeTotalPages',res.data.totalPage,mypage);
           }else{
            $('#pagination-demo').hide();
              $('.box').show();
           }
       }
    }
 })
}

 //页面一加载就展示文章条目数据
 getData(mypage, function (res) {
                $('#pagination-demo').twbsPagination({
                    totalPages: res.data.totalPage,
                    visiblePages: 7,
                    first: '首页',
                    prev: '上一页',
                    next: '下一页',
                    last: '尾页',
                    onPageClick: function (event, page) {
                        mypage = page;
                        getData(mypage, function(){});
                    }
                });
            })

//筛选按钮注册点击事件
          $('#btnSearch').on('click',function(e){
              e.preventDefault();
             getData(mypage,function(res){
                //  筛选之后 要改一下页数
              $('#pagination-demo').twbsPagination('changeTotalPages',res.data.totalPage,1)
             })
          })

             // 实现点击删除
        $('tbody').on('click','.btn-danger',function(){
            let id = $(this).attr('data-id');
            let ans = confirm('你确定要删除吗');
            if(ans){
                $.post({
                url : BigNew.article_delete,
                data : {
                    id : id
                },
                success : function(res){
                    getData(mypage,function(res){
                    $('#pagination-demo').twbsPagination('changeTotalPages',res.data.totalPage,mypage);
                   });
                }
            })
            }
        })

        }); 
</script>

</html>